package com.muqi.app.qlearn.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseIntArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.easemob.easeui.R;
import com.easemob.easeui.domain.EaseUser;
import com.easemob.easeui.utils.EaseUserUtils;
import com.easemob.util.EMLog;
import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.user.db.MyClassStudent;

public class MainListAdapter extends ArrayAdapter<MyClassStudent>{
	private static final String TAG = "ContactAdapter";
	List<String> list;
	List<MyClassStudent> userList;
	List<MyClassStudent> copyUserList;
	private LayoutInflater layoutInflater;
	private int res;
	private Context context;

	public MainListAdapter(Context context, int resource,
			List<MyClassStudent> objects) {
		super(context, resource, objects);
		this.context=context;
		this.res = resource;
		this.userList = objects;
		copyUserList = new ArrayList<MyClassStudent>();
		copyUserList.addAll(objects);
		layoutInflater = LayoutInflater.from(context);
	}

	private static class ViewHolder {
		ImageView avatar;
		TextView nameView;
		TextView headerView;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			if (res == 0)
				convertView = layoutInflater.inflate(R.layout.ease_row_contact,
						null);
			else
				convertView = layoutInflater.inflate(res, null);
			holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
			holder.nameView = (TextView) convertView.findViewById(R.id.name);
			holder.headerView = (TextView) convertView
					.findViewById(R.id.header);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		MyClassStudent user = userList.get(position);
		if (user == null)
			Log.d("ContactAdapter", position + "");
		
		String username = user.student_name;
		String header = user.initialLetter;

		if (position == 0 || header != null
				&& !header.equals(getItem(position - 1).initialLetter)) {
			if (TextUtils.isEmpty(header)) {
				holder.headerView.setVisibility(View.GONE);
			} else {
				holder.headerView.setVisibility(View.VISIBLE);
				holder.headerView.setText(header);
			}
		} else {
			holder.headerView.setVisibility(View.GONE);
		}
		holder.nameView.setText(username);
		// 设置头像
		GlideImageUtils.setAvatar(context, user.student_avatar,
				holder.avatar);

		if (primaryColor != 0)
			holder.nameView.setTextColor(primaryColor);
		if (primarySize != 0)
			holder.nameView
					.setTextSize(TypedValue.COMPLEX_UNIT_PX, primarySize);
		if (initialLetterBg != null)
			holder.headerView.setBackgroundDrawable(initialLetterBg);
		if (initialLetterColor != 0)
			holder.headerView.setTextColor(initialLetterColor);

		return convertView;
	}

	@Override
	public MyClassStudent getItem(int position) {
		return userList.get(position);
	}

	@Override
	public int getCount() {
		return userList.size();
	}

	
	protected int primaryColor;
	protected int primarySize;
	protected Drawable initialLetterBg;
	protected int initialLetterColor;

	public MainListAdapter setPrimaryColor(int primaryColor) {
		this.primaryColor = primaryColor;
		return this;
	}

	public MainListAdapter setPrimarySize(int primarySize) {
		this.primarySize = primarySize;
		return this;
	}

	public MainListAdapter setInitialLetterBg(Drawable initialLetterBg) {
		this.initialLetterBg = initialLetterBg;
		return this;
	}

	public MainListAdapter setInitialLetterColor(int initialLetterColor) {
		this.initialLetterColor = initialLetterColor;
		return this;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

}
